/**
 * BaseActivity.java
 * 
 * Purpose              :
 * 
 * Optional info        : 
 *
 * @author              : Hoang Viet
 * 
 * @date                : Jun 5, 2012
 * 
 * @lastChangedRevision : 
 *
 * @lastChangedDate     :
 *
 */
package com.applink.sell_proof;

import lib.redmanit.pager.TabPageIndicator;
import android.content.BroadcastReceiver;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.SimpleOnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;

import com.actionbarsherlock.app.ActionBar;
import com.applink.sell_proof.broadcast_receiver.PopupReceiver;
import com.applink.sell_proof.dialog.EAlertDialog;
import com.applink.sell_proof.dialog.WaitingDialog;
import com.applink.sell_proof.fragment.adapter.BaseFragmentPagerAdapter;
import com.applink.sell_proof.util.Utility;

/**
 * @author Hoang Viet
 * 
 */
public abstract class BasePagerActivity extends AbstractActivity {

	private ViewPager mPager;
	private TabPageIndicator mIndicator;
	private BaseFragmentPagerAdapter mFragmentAdaper;
	private SimpleOnPageChangeListener mOnPageChangeListener;
	private BroadcastReceiver mBroadcastReceiver;

	public static String PAGER_INDEX = "pager_index";

	public static final int SELL_ACTIVITY = 0;
	public static final int REWARD_ACTIVITY = 1;
	public static final int LEARN_ACTIVITY = 2;
	public static final int LIFE_ACTIVITY = 3;
	public static final int SUPPORT_ACTIVITY = 4;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// call super constructor
		super.onCreate(savedInstanceState);

		// Inflating the layout
		setContentView(R.layout.main_pager);

		// set soft inputs
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

		// binding all the variables to it View value
		findViews();

		ActionBar actionbar = getSupportActionBar();
		actionbar.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.bg_topbar));
		actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);
		mPager.setAdapter(mFragmentAdaper = getFragmentPagerAdapter());
		mIndicator.setViewPager(mPager);
		mIndicator.setOnPageChangeListener(new MyPageChangeListener());
		final int index = getIntent().getIntExtra(PAGER_INDEX,
				getDefaultPagerIndex());
		setPagerIndex(index);
	}

	@Override
	protected void onResume() {
		super.onResume();
		IntentFilter intentFilter = new IntentFilter(
				"com.google.android.c2dm.intent.RECEIVE_FORWARD");
		intentFilter.setPriority(1);
		mBroadcastReceiver = PopupReceiver.instance();
		registerReceiver(mBroadcastReceiver, intentFilter);

		setSelectedTab();
	}

	@Override
	protected void onPause() {
		super.onPause();
		unregisterReceiver(mBroadcastReceiver);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		int index = intent.getIntExtra(PAGER_INDEX, -1);
		if (index != -1 && index != mPager.getCurrentItem())
			mPager.setCurrentItem(index);
	}

	/***
	 * binding all the variables to it View value
	 */
	@Override
	protected void findViews() {
		mPager = (ViewPager) findViewById(R.id.pager);
		mIndicator = (TabPageIndicator) findViewById(R.id.indicator);

		findViewById(R.id.deal_tab).setOnClickListener(this);
		findViewById(R.id.reward_tab).setOnClickListener(this);
		findViewById(R.id.learn_tab).setOnClickListener(this);
		findViewById(R.id.custservice_tab).setOnClickListener(this);
		findViewById(R.id.announce_tab).setOnClickListener(this);
	}

	public void setActionbarTitle(String title) {
		getSupportActionBar().setTitle(title);
	}

	protected void toggleIndicator(boolean showIndicator) {
		if (!showIndicator)
			((View) mIndicator).setVisibility(View.GONE);
		else
			((View) mIndicator).setVisibility(View.VISIBLE);
	}

	protected void addSimpleOnPageChangeListener(
			SimpleOnPageChangeListener onPageChangeListener) {
		mOnPageChangeListener = onPageChangeListener;
	}

	protected void removeSimpleOnPageChangeListener() {
		mOnPageChangeListener = null;
	}

	public int getCurrentPagerPosition() {
		return mPager.getCurrentItem();
	}

	public void setPagerIndex(int index) {
		if (index != mPager.getCurrentItem())
			mPager.setCurrentItem(index);
	}

	public void invalidateIndicator() {
		mIndicator.notifyDataSetChanged();
	}

	protected void setOffscreenPageLimit(int numOfPage) {
		mPager.setOffscreenPageLimit(numOfPage);
	}

	protected void setOnPageChangeListener(
			ViewPager.SimpleOnPageChangeListener onPageChangeListener) {
		mIndicator.setOnPageChangeListener(onPageChangeListener);
	}

	public Fragment getFragmentAt(int index) {
		return mFragmentAdaper.getFragmentAt(index);
	}

	protected abstract BaseFragmentPagerAdapter getFragmentPagerAdapter();

	protected abstract int getDefaultPagerIndex();

	protected void reCreateFragments() {
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		int size = mFragmentAdaper.getCount();
		for (int i = 0; i < size; i++)
			ft.remove(mFragmentAdaper.getFragmentAt(i));
		ft.commit();
		mPager.setAdapter(mFragmentAdaper = getFragmentPagerAdapter());
		mIndicator.setViewPager(mPager);
		mPager.setCurrentItem(getDefaultPagerIndex());
		mIndicator.setCurrentItem(getDefaultPagerIndex());
	}

	private class MyPageChangeListener extends
			ViewPager.SimpleOnPageChangeListener {
		@Override
		public void onPageSelected(int position) {
			if (mOnPageChangeListener != null) {
				mOnPageChangeListener.onPageSelected(position);
			}
		}
	}

	public void showProgressDialog(int containerId) {
		Fragment waitingDialog = new WaitingDialog(R.color.white);
		FragmentTransaction transaction = getSupportFragmentManager()
				.beginTransaction();
		transaction.replace(containerId, waitingDialog, WaitingDialog.TAG);
		transaction.commit();
	}

	public void hideProgressDialog(int containerId) {

		Fragment waitingDialog = getSupportFragmentManager().findFragmentById(
				containerId);
		if (waitingDialog != null) {
			((DialogFragment) waitingDialog).dismiss();
		}
	}	

	public void showAlertDialog(DialogInterface.OnClickListener listener,
			String title, String navigative, String positive, String message,
			boolean cancelable) {
		WaitingDialog waitingDialog = (WaitingDialog) getSupportFragmentManager()
				.findFragmentByTag(WaitingDialog.TAG);
		if (waitingDialog != null && waitingDialog.isFullScreen()) {
			waitingDialog.dismiss();
		}
		DialogFragment dialogFragment = new EAlertDialog(listener, title,
				navigative, positive, message);
		dialogFragment.setCancelable(cancelable);
		dialogFragment.show(getSupportFragmentManager(), null);
	}

	public abstract void setSelectedTab();

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (Utility.popupWindow != null && Utility.popupWindow.isShowing()) {
				Utility.popupWindow.dismiss();
				Utility.popupWindow = null;
				return true;
			}else{
				moveTaskToBack(true);
				return true;
			}
			
		}
		return super.onKeyUp(keyCode, event);
	}	

	public abstract int getActivityIndex();
}
